//
//  ViewController.m
//  CountriesTableView
//
//  Created by student on 16/3/3.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tabelView;

@property (nonatomic,strong) NSArray *countryName;
@property (nonatomic,strong) NSArray *nationalFlag;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.countryName = [[NSArray alloc] initWithObjects:@"A1-南非",@"A2-墨西哥",
                        @"B1-阿根廷",@"B2-尼日利亚",@"C1-英格兰",@"C2-美国",
                        @"D1-德国",@"D2-澳大利亚",@"E1-荷兰",@"E2-丹麦",
                        @"G1-巴西",@"G2-朝鲜",@"H1-西班牙",@"H2-瑞士",nil];
    self.nationalFlag = [[NSArray alloc] initWithObjects:@"SouthAfrica.png",@"Mexico.png",
                         @"Argentina.png",@"Nigeria.png",@"England.png",@"USA.png",
                         @"Germany.png",@"Australia.png",@"Holland.png",@"Denmark.png",                      @"Brazil.png",@"NorthKorea.png",@"Spain.png",@"Switzerland.png",nil];
    
    [self.tabelView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
}

//第几段有几行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.countryName.count;
}


//第几段第几行的单元格内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    cell.textLabel.text = self.countryName[indexPath.row];
    cell.imageView.image = [UIImage imageNamed:self.nationalFlag[indexPath.row]];
    return cell;
}


#pragma mark - 为表格提供事件处理
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = indexPath.row;
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"你选择了" message:self.countryName[row] preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    
    [alert addAction:action];
    
    [self presentViewController:alert animated:YES completion:nil];
}
@end
